﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace TestResults
{
    public class SampleData
    {
        /// <summary>
        /// Get a sample taken test
        /// </summary>
        /// <returns>Sample test</returns>
        public static Test get_sample()
        {
            Test test = new Test();

            Question[] q = new Question[10];

            q[0] = _populateQuestion(
                "What is your name",
                new string[3] { "Aurthur King of the Brittons", "Sir Galahad", "Sir Robbin" },
                2, 2);

            q[1] = _populateQuestion(
                "What is your Quest?",
                new string[3] { "seek the grail", "find peace", "none of the above" },
                1, 1);

            q[2] = _populateQuestion(
                "What is your favorite color?",
                new string[3] { "red", "green", "blue" },
                3, 1);

            q[3] = _populateQuestion(
                "What is the answer to Life, the Universe and Everything?",
                new string[4] { "42", "24", "1", "infinity" },
                1, 1);

            q[4] = _populateQuestion(
                "Who is buried in Grant's tomb?",
                new string[3] { "nobody", "grant", "a mummy" },
                2, 2);

            q[5] = _populateQuestion(
                "What does Marcellus Wallace look like?",
                new string[3] { "what?", "I can tell you what he doesn't look like...", "can you repeat the question?" },
                2, 1);

            q[6] = _populateQuestion(
                "According to Conan, what is best in life?",
                new string[4] { 
                    "To crush your enemies", 
                    "To see them driven before you", 
                    "To hear the lamentation of their women", 
                    "All of the above" },
                4, 4);

            q[7] = _populateQuestion(
                "How much wood would a woodchuck chuck if a woodchuck could chuck wood",
                new string[3] { "a lot", "a little", "huh?" },
                1, 3);

            q[8] = _populateQuestion(
                "Who broke the dam?",
                new string[4] { "Kenny", "Stan", "Kyle", "Cartman" },
                2, 2);

            q[9] = _populateQuestion(
                "Who is Eric Cartman's Father?",
                new string[4] { "MR. Garrison", "Mr. Mackey", "The Denver Broncos", "His Mother" },
                4, 4);

            test.Questions = new List<Question>(q);

            test.Score = _gradeTest(test);

            return test;
        }

        /// <summary>
        /// Populate a question object with the supplied parameters
        /// </summary>
        /// <param name="question_text">Text of the question</param>
        /// <param name="answers">Possible answers</param>
        /// <param name="correct_index">index on the correct answer in the list of answers</param>
        /// <param name="chosen_index">index on the selected answer in the list of answers</param>
        /// <returns></returns>
        private static Question _populateQuestion(
            String question_text,
            string[] answers,
            int correct_index,
            int chosen_index)
        {
            Question question = new Question();

            question.QuestionText = question_text;
            question.Answers = new List<string>(answers);
            question.CorrectIndex = correct_index;
            question.SelectedIndex = chosen_index;

            return question;
        }

        /// <summary>
        /// Grade a supplied test
        /// </summary>
        /// <param name="test">number of correct answers</param>
        /// <returns></returns>
        private static int _gradeTest(Test test)
        {
            int score = 0;

            foreach (Question question in test.Questions)
            {
                if (question.SelectedIndex == question.CorrectIndex)
                {
                    score++;
                }
            }

            return score;
        }
    }
}